//
//  netSocket.h
//
//  Created by fox on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#ifndef netSocket_h
#define netSocket_h


#include "netIOBuffer.h"


namespace FOXSDK
{
    class netServer;
    
    
    enum SocketRecvType
    {
        FSRS_NULL = 0,
        FSRS_HEAD = 1,
        FSRS_BODY = 2,
        FSRS_COUNT = 3
    };
    
    
//    struct netSocketHead
//    {
//        fuint16 size;
//        fuint16 type;
//    };
    
    
    class netSocket : public tcp::socket
    {
    public:
        
        friend class netServer;
        
        netSocket( io_service& io_service );
        
        fvoid   RecvMsg( const boost::system::error_code& ec, size_t bytes_transferred );
        fvoid   SendMsg( const boost::system::error_code& ec, size_t bytes_transferred );
        
        fvoid   ParkMsg( netMsgHead* head );
        fvoid   SendMsg();
        
        fint32  ReadMsg( netMsgHead** head );
        fvoid   RemoveMsg( int len );
        
        
        fint32  IsVaild()
        {
            return mVaild;
        }
        
        fvoid   Close()
        {
            mClose = 1;
        }
        
    protected:
        
        deadline_timer mTimer;
        
        virtual ~netSocket();
        
        fvoid   Run();
        
        virtual void    close();
        
        fvoid   Clear();
        
        //mutex   mMutex;
        
        fint32  mVaild;
        
        fvoid   ReadHead();
        fvoid   ReadBody();
        
        void    HandleWait( const boost::system::error_code& error );
        
        netServer*  mServer;
        
        netIOBuffer mIBuffer;
        netIOBuffer mOBuffer;
        
        mutable_buffers_1 mHeadBuffer;
        mutable_buffers_1 mBodyBuffer;
        mutable_buffers_1 mSendBuffer1;
        
        fbyte   mRecvBuffer[ MAX_SOCKET_BUFFER ];
        fint32  mRecvStage;
        fint32  mBodyLen;
        
        fbyte   mSendBuffer[ MAX_SOCKET_BUFFER ];
        fint32  mSend;
        
        fint32  mClose;
    };
    
    typedef list<netSocket*> SocketList;
    typedef list<netSocket*>::iterator SocketListIter;
    typedef vector<netSocket*> SocketVector;
    typedef vector<netSocket*>::iterator SocketVectorIter;
    

    
}



#endif
